package edu.wpi.first.wpilibj.util;

import java.util.*;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;

/**
 *
 * @author Bloodshot
 */
public class ArrayMap <K, V> implements Map<K, V> {

    private List <K> keyList;
    private List <V> valueList;

    public ArrayMap() {
        this (8);
    }
    
    public ArrayMap(int initCapacity){
        keyList = new ArrayList <K>(initCapacity);
        valueList = new ArrayList <V>(initCapacity);
    }
    
    public ArrayMap(List<K> l1, List<V> l2){
        keyList = l1;
        valueList = l2;
    }
    
    @Override
    public void clear() {
        keyList.clear();
        valueList.clear();
    }

    @Override
    public boolean containsKey(Object key) {
        return keyList.contains(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return valueList.contains(value);
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        ArraySet <Entry<K, V>> returnSet = new ArraySet <Entry<K, V>>();
        for (int x = 0;x < keyList.size();x++){
            returnSet.add(new SimpleEntry<K, V>(keyList.get(x), valueList.get(x)));
        }
        return returnSet;
    }

    @Override
    public V get(Object key) {
        return valueList.get (keyList.indexOf(key));
    }

    @Override
    public boolean isEmpty() {
        return keyList.isEmpty();
    }

    @Override
    public Set<K> keySet() {
        return new ArraySet(keyList);
    }

    @Override
    public V put(K key, V value) {
        keyList.add (key);
        valueList.add (keyList.indexOf (key), value);
        return value;
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {
        for (Iterator<? extends K> it = m.keySet().iterator();it.hasNext();){
            K k = it.next();
            put (k, m.get(k));
        }
    }

    @Override
    public V remove(Object key) {
        V v = get(key);
        valueList.remove(keyList.indexOf (key));
        keyList.remove(key);
        return v;
    }

    @Override
    public int size() {
        return keyList.size();
    }

    @Override
    public Collection<V> values() {
        return valueList;
    }
    
}
